/* 
 * This code is part of the notepad application, a sample application
 * for the SmartLink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * It is a slightly modified version of the original notepad sample application 
 * distributed by Google as part of the Android SDK, see
 * 
 * 		http://code.google.com/android/samples/NotePad/index.html.
 * 
 * The modifications have the purpose of making the notepad application
 * acting as a "smartlink client" and "smartlink server" application.
 * What this means and the necessary requirements can be found in the documentation
 * of the SmartLink project. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.notepad;

import org.smartlink.Server;

import com.google.provider.NotePad;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/** Activity to return the note text to smartlink */
public class NoteResultActivity extends Activity {

	private static final String TAG = "NoteResultActivity";

	public NoteResultActivity() {
		super();
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		// setContentView(R.layout.translucent_background);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Intent intent = getIntent();
		String action = intent.getAction();
		if (action.equals(Server.USE_TARGET_ACTION)) {
			setResult(RESULT_OK, getResult(intent.getData()));
			finish();
		} else {
			Log.w(TAG, "started with unexpected action: " + action);
			return;
		}
	}

	protected String getResult(Uri uri) {
		return getColumn(uri, NotePad.Notes.NOTE);
	}

	/** gets the specified column of the contact database entry. */
	protected String getColumn(Uri uri, String column) {
		String[] projection = new String[] { column };
		Cursor cur = managedQuery(uri, projection, // Which columns to return.
				null, // WHERE clause
				null); // Order-by clause.
		if (!cur.first()) {
			return null;
		}
		return cur.getString(cur.getColumnIndex(column));
	}
}